package com.pz.cloud.framework.base.config;

import com.codingapi.txlcn.tc.support.DTXUserControls;
import com.codingapi.txlcn.tracing.TracingContext;
import com.pz.cloud.framework.base.model.BaseResult;
import com.pz.cloud.framework.base.service.BaseBizController;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

/**
 * 服务降级处理
 * @author 3hmzz
 */
public abstract class BaseFallback<T,C extends BaseBizController> implements  BaseBizController<T> ,FallbackFactory<C> {

    private Logger logger = LoggerFactory.getLogger(BaseFallback.class);

    @Autowired
    private C c;

    public C create(Throwable throwable) {
        logger.error("服务调用异常：",throwable);
        try{  //分布式事务回滚
            String groupId = TracingContext.tracing().groupId();
            DTXUserControls.rollbackGroup(groupId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return c;
    }

    @Override
    public BaseResult findList(Map param) {
        return  BaseResult.error("服务异常");
    }
    @Override
    public BaseResult findPage(Map param) {
        return  BaseResult.error("服务异常");
    }
    @Override
    public BaseResult findById(@RequestBody Map<String,String> parms){
        return  BaseResult.error("服务异常");
    }
    @Override
    public BaseResult delete(@RequestBody Map<String,String> param){
        return  BaseResult.error("服务异常");
    }
@   Override
    public BaseResult save(@RequestBody Map<String,String> param){
         return  BaseResult.error("服务异常");
    }
}
